package com.bkgy.controller.tbtcmthkjudge.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.controller.platecut.mapper.TbPltcmAscMapper;
import com.bkgy.controller.tbtcmthkjudge.dto.TbTcmThkJudgeRequest;
import com.bkgy.controller.tbtcmthkjudge.entity.TbTcmThkJudge;
import com.bkgy.controller.tbtcmthkjudge.entity.TbTcmThkJudgeReq;
import com.bkgy.controller.tbtcmthkjudge.entity.TbTcmThkJudgeResp;
import com.bkgy.controller.tbtcmthkjudge.entity.TbTcmThkJudgeResultCollect;
import com.bkgy.controller.tbtcmthkjudge.mapper.TbTcmThkJudgeMapper;
import com.bkgy.controller.tbtcmthkjudge.service.TbTcmThkJudgeService;
import com.bkgy.sys.common.entity.ResultData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * Author:Lipeng Zhao
 * Description:
 *
 * @Date:Created In 14:41 2025/5/19
 */
@Service
@Slf4j
public class TbTcmThkJudgeServiceImpl extends ServiceImpl<TbTcmThkJudgeMapper, TbTcmThkJudge> implements TbTcmThkJudgeService {

    @Autowired
    private TbTcmThkJudgeMapper tbTcmThkJudgeMapper;

    @Autowired
    private TbPltcmAscMapper tbPltcmAscMapper;

    public ResultData findDataByPage(String data) {
        try {
            TbTcmThkJudgeReq tbTcmThkJudgeRequest = JSON.parseObject(data, TbTcmThkJudgeReq.class);
            JSONObject jsonObject  = null;
            Integer pageIndex = tbTcmThkJudgeRequest.getPageIndex();
            Integer pageSize = tbTcmThkJudgeRequest.getPageSize();

            if(null == pageIndex || null == pageSize){
                return ResultData.ResultDataFaultSelf("分页参数没有传", null);
            }



//            QueryWrapper<TbTcmThkJudge> queryWrapper = new QueryWrapper<TbTcmThkJudge>();
//            if(!StringUtils.isEmpty(jsonObject.get("prodThicknessMin"))){
//                queryWrapper.ge("EX_THK",jsonObject.get("prodThicknessMin"));
//            }
//            if(!StringUtils.isEmpty(jsonObject.get("prodThicknessMax"))){
//                queryWrapper.le("EX_THK",jsonObject.get("prodThicknessMax"));
//            }
//
//            if(!StringUtils.isEmpty(jsonObject.get("materialThicknessMin"))){
//                queryWrapper.ge("EN_THK",jsonObject.get("materialThicknessMin"));
//            }
//
//            if(!StringUtils.isEmpty(jsonObject.get("materialThicknessMax"))){
//                queryWrapper.le("EN_THK",jsonObject.get("materialThicknessMax"));
//            }
//
//            if(!StringUtils.isEmpty(jsonObject.get("materialWidthMin"))){
//                queryWrapper.ge("EN_WIDTH",jsonObject.get("materialWidthMin"));
//            }
//
//            if(!StringUtils.isEmpty(jsonObject.get("materialWidthMax"))){
//                queryWrapper.le("EN_WIDTH",jsonObject.get("materialWidthMax"));
//            }
//
//            if(!StringUtils.isEmpty(jsonObject.get("steelGrade"))){
//                queryWrapper.eq("STEEL_GRADE",jsonObject.get("steelGrade"));
//            }
//
//            if(!StringUtils.isEmpty(jsonObject.get("hotRollNo"))){
//                queryWrapper.eq("HOT_COIL_ID",jsonObject.get("hotRollNo"));
//            }
//
//            if(!StringUtils.isEmpty(jsonObject.get("coldRollNo"))){
//                queryWrapper.eq("COLD_COIL_ID",jsonObject.get("coldRollNo"));
//            }
//
//
//            if(!StringUtils.isEmpty(jsonObject.get("startDate"))){
//                queryWrapper.ge("CREATE_TIME",jsonObject.get("startDate"));
//            }
//
//            if(!StringUtils.isEmpty(jsonObject.get("endDate"))){
//                queryWrapper.le("CREATE_TIME",jsonObject.get("endDate"));
//            }
//
//            if(!StringUtils.isEmpty(jsonObject.get("thkFlag"))){
//                queryWrapper.eq("THK_FLAG",jsonObject.get("thkFlag"));
//            }
//
//            queryWrapper.orderByDesc("CREATE_TIME");
            Page<TbTcmThkJudge> page = new Page<TbTcmThkJudge>(pageIndex, pageSize);
            //IPage<TbTcmThkJudge> list = tbTcmThkJudgeMapper.selectPage(page,queryWrapper);


            IPage<TbTcmThkJudgeResp> list = tbTcmThkJudgeMapper.tbTcm2ThkJudge(page,tbTcmThkJudgeRequest);


//            IPage<TbTcmThkJudgeResp> list1=new Page<>();
//            List<TbTcmThkJudgeResp> list2=new ArrayList<>();
//            for(int i=0;i<list.getSize();i++){
//                TbTcmThkJudge tbTcmThkJudge=list.getRecords().get(i);
//                TbPltcmAsc tbPltcmAsc=tbPltcmAscMapper.selectByEntID(tbTcmThkJudge.getHotCoilID());
//                TbTcmThkJudgeResp tbTcmThkJudgeResp=new TbTcmThkJudgeResp();
//                BeanUtils.copyProperties(tbTcmThkJudge,tbTcmThkJudgeResp);
//                tbTcmThkJudgeResp.setFlataveAlarmLengthHead(tbPltcmAsc.getFlataveAlarmLengthHead());
//                tbTcmThkJudgeResp.setFlataveAlarmLengthMiddle(tbPltcmAsc.getFlataveAlarmLengthMiddle());
//                tbTcmThkJudgeResp.setFlataveAlarmLengthTail(tbPltcmAsc.getFlataveAlarmLengthTail());
//                tbTcmThkJudgeResp.setCloseFlag(tbPltcmAsc.getClostFlag());
//                list2.add(tbTcmThkJudgeResp);
//                list1.setRecords(list2);
//            }


//            List<TbTcmThkJudge> resultNoPage = tbTcmThkJudgeMapper.selectList(queryWrapper);
//            BigDecimal sum1 = resultNoPage.stream().filter(x -> !ObjectUtils.isEmpty(x.getThkHeadToleAbsLen())).map(TbTcmThkJudge::getThkHeadToleAbsLen).reduce(BigDecimal.ZERO, BigDecimal::add);
//            BigDecimal sum2 = resultNoPage.stream().filter(x -> !ObjectUtils.isEmpty(x.getThkTailToleAbsLen())).map(TbTcmThkJudge::getThkTailToleAbsLen).reduce(BigDecimal.ZERO, BigDecimal::add);
//
//            int count = resultNoPage.size(); // 获取元素数量
//            BigDecimal ave1 = count > 0 ? sum1.divide(BigDecimal.valueOf(count), 2, RoundingMode.HALF_UP) : BigDecimal.ZERO; // 计算平均值，处理除零情况
//
//            BigDecimal ave2 = count > 0 ? sum2.divide(BigDecimal.valueOf(count), 2, RoundingMode.HALF_UP) : BigDecimal.ZERO; // 计算平均值，处理除零情况
//
//
//
//            TbTcmThkJudgeResultCollect collect = new TbTcmThkJudgeResultCollect();
//            collect.setSum1(sum1);
//            collect.setSum2(sum2);
//            collect.setAve1(ave1);
//            collect.setAve2(ave2);

            return ResultData.ResultDataSuccess(list,(int) list.getTotal());
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }

    }

    public ResultData findThkJudgeByPage(String data) {
        try {
            TbTcmThkJudgeReq tbTcmThkJudgeRequest = JSON.parseObject(data, TbTcmThkJudgeReq.class);
            JSONObject jsonObject  = null;
            Integer pageIndex = tbTcmThkJudgeRequest.getPageIndex();
            Integer pageSize = tbTcmThkJudgeRequest.getPageSize();

            if(null == pageIndex || null == pageSize){
                return ResultData.ResultDataFaultSelf("分页参数没有传", null);
            }

            Page<TbTcmThkJudge> page = new Page<TbTcmThkJudge>(pageIndex, pageSize);


            IPage<TbTcmThkJudgeResp> list = tbTcmThkJudgeMapper.findThkJudgeByPage(page,tbTcmThkJudgeRequest);

            return ResultData.ResultDataSuccess(list,(int) list.getTotal());
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }

    }


    public ResultData findDataNoPage(String data) {
        try {
            QueryWrapper<TbTcmThkJudge> queryWrapper = new QueryWrapper<TbTcmThkJudge>();
            List<TbTcmThkJudge> resultNoPage = tbTcmThkJudgeMapper.selectList(queryWrapper);
            BigDecimal sum1 = resultNoPage.stream().filter(x -> !ObjectUtils.isEmpty(x.getThkHeadToleAbsLen())).map(TbTcmThkJudge::getThkHeadToleAbsLen).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal sum2 = resultNoPage.stream().filter(x -> !ObjectUtils.isEmpty(x.getThkTailToleAbsLen())).map(TbTcmThkJudge::getThkTailToleAbsLen).reduce(BigDecimal.ZERO, BigDecimal::add);

            int count = resultNoPage.size(); // 获取元素数量
            BigDecimal ave1 = count > 0 ? sum1.divide(BigDecimal.valueOf(count), 2, RoundingMode.HALF_UP) : BigDecimal.ZERO; // 计算平均值，处理除零情况

            BigDecimal ave2 = count > 0 ? sum2.divide(BigDecimal.valueOf(count), 2, RoundingMode.HALF_UP) : BigDecimal.ZERO; // 计算平均值，处理除零情况

            TbTcmThkJudgeResultCollect collect = new TbTcmThkJudgeResultCollect();
            collect.setSum1(sum1);
            collect.setSum2(sum2);
            collect.setAve1(ave1);
            collect.setAve2(ave2);

            return ResultData.ResultDataSuccess(collect);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }

    }


    public ResultData findByThkFlagAndWidFlag(String data){
        try {
            TbTcmThkJudgeRequest tbTcmThkJudgeRequest = JSON.parseObject(data, TbTcmThkJudgeRequest.class);
            JSONObject jsonObject  = null;
            Integer pageIndex = tbTcmThkJudgeRequest.getPageIndex();
            Integer pageSize = tbTcmThkJudgeRequest.getPageSize();

            if(null == pageIndex || null == pageSize){
                return ResultData.ResultDataFaultSelf("分页参数没有传", null);
            }

            if(null!=tbTcmThkJudgeRequest.getCondition()){
                jsonObject  = JSON.parseObject(tbTcmThkJudgeRequest.getCondition().toString());
            }

            TbTcmThkJudge tbTcmThkJudge=tbTcmThkJudgeRequest.getTbTcmThkJudge();
            Page<TbTcmThkJudge> page = new Page<TbTcmThkJudge>(pageIndex, pageSize);
            IPage<TbTcmThkJudge> list = tbTcmThkJudgeMapper.selectByThkFlagAndWidFlag(page, tbTcmThkJudge);




            return ResultData.ResultDataSuccess(list,(int) list.getTotal());
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }

    }

    /**
     * 酸轧厚度型异常明细
     * @param data json字符串
     */
    public ResultData tbTcm2ThkJudge(String data){
        try {
            TbTcmThkJudgeRequest tbTcmThkJudgeRequest = JSON.parseObject(data, TbTcmThkJudgeRequest.class);
            JSONObject jsonObject  = null;
            Integer pageIndex = tbTcmThkJudgeRequest.getPageIndex();
            Integer pageSize = tbTcmThkJudgeRequest.getPageSize();

            if(null == pageIndex || null == pageSize){
                return ResultData.ResultDataFaultSelf("分页参数没有传", null);
            }

            if(null!=tbTcmThkJudgeRequest.getCondition()){
                jsonObject  = JSON.parseObject(tbTcmThkJudgeRequest.getCondition().toString());
            }

            TbTcmThkJudge tbTcmThkJudge=tbTcmThkJudgeRequest.getTbTcmThkJudge();
            Page<TbTcmThkJudge> page = new Page<TbTcmThkJudge>(pageIndex, pageSize);
            IPage<TbTcmThkJudge> list = tbTcmThkJudgeMapper.tbTcm2ThkJudges(page, tbTcmThkJudge);

            return ResultData.ResultDataSuccess(list,(int) list.getTotal());
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }
    }



}
